<?php
/**
 * Class that operate on table 'classifieds_category'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class ClassifiedsCategoryMySqlDAO implements ClassifiedsCategoryDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ClassifiedsCategoryMySql 
	 */
	public function load($classifiedsCategory){
		$sql = 'SELECT * FROM classifieds_category WHERE category_id = "'.$classifiedsCategory->category_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}
	
	public function loadSubcategory($classifiedsCategory){
		$sql = 'SELECT * FROM classifieds_category WHERE category_id = "'.$classifiedsCategory->category_id.'" and parent_id > 0';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	
	
	public function category_list(){
		$sql = 'SELECT * FROM classifieds_category WHERE parent_id = "0"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function subcategory_list($classifiedsCategory){
		$sql = 'SELECT * FROM classifieds_category WHERE parent_id = "'.$classifiedsCategory->category_id.'" and parent_id > 0';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllSearchBy($classifiedsCategory){
		$sql = 'SELECT * FROM classifieds_category WHERE '.$classifiedsCategory->search_by.' like "%'.$classifiedsCategory->search_by_value.'%" order by '.$classifiedsCategory->order_by.' '.$classifiedsCategory->sort_order.' limit '.$classifiedsCategory->results_per_page.' offset '.$classifiedsCategory->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function SearchByCount($classifiedsCategory){
		$sql = 'SELECT count(*) FROM classifieds_category WHERE '.$classifiedsCategory->search_by.' like "%'.$classifiedsCategory->search_by_value.'%" order by '.$classifiedsCategory->order_by.' '.$classifiedsCategory->sort_order.' limit '.$classifiedsCategory->results_per_page.' offset '.$classifiedsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function queryAll($classifiedsCategory){
		$sql = 'SELECT * FROM classifieds_category order by '.$classifiedsCategory->order_by.' '.$classifiedsCategory->sort_order.' limit '.$classifiedsCategory->results_per_page.' offset '.$classifiedsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function queryAllCount($classifiedsCategory){
		$sql = 'SELECT count(*) FROM classifieds_category order by '.$classifiedsCategory->order_by.' '.$classifiedsCategory->sort_order.' limit '.$classifiedsCategory->results_per_page.' offset '.$classifiedsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	
	
	public function subcategorySearchBy($classifiedsCategory){
		$sql = 'SELECT * FROM classifieds_category WHERE '.$classifiedsCategory->search_by.' like "%'.$classifiedsCategory->search_by_value.'%" and parent_id > "0" order by '.$classifiedsCategory->order_by.' '.$classifiedsCategory->sort_order.' limit '.$classifiedsCategory->results_per_page.' offset '.$classifiedsCategory->offset.'';
		print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function subcategoryCount($classifiedsCategory){
		$sql = 'SELECT count(*) FROM classifieds_category WHERE '.$classifiedsCategory->search_by.' like "%'.$classifiedsCategory->search_by_value.'%" and parent_id > 0 order by '.$classifiedsCategory->order_by.' '.$classifiedsCategory->sort_order.' limit '.$classifiedsCategory->results_per_page.' offset '.$classifiedsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function queryAllSubcategory($classifiedsCategory){
		$sql = 'SELECT * FROM classifieds_category WHERE parent_id > 0 order by '.$classifiedsCategory->order_by.' '.$classifiedsCategory->sort_order.' limit '.$classifiedsCategory->results_per_page.' offset '.$classifiedsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function queryAllSubcategoryCount($classifiedsCategory){
		$sql = 'SELECT count(*) FROM classifieds_category WHERE parent_id > 0 order by '.$classifiedsCategory->order_by.' '.$classifiedsCategory->sort_order.' limit '.$classifiedsCategory->results_per_page.' offset '.$classifiedsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param classifiedsCategory primary key
 	 */
	public function delete($classifiedsCategory){
		$sql = 'UPDATE classifieds_category SET status = "'.$classifiedsCategory->status.'" WHERE category_id = "'.$classifiedsCategory->category_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	public function deleteSubcategory($classifiedsCategory){
		$sql = 'UPDATE classifieds_category SET status = "'.$classifiedsCategory->status.'" WHERE category_id = "'.$classifiedsCategory->category_id.'" and parent_id > 0';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param ClassifiedsCategoryMySql classifiedsCategory
 	 */
	public function insert($classifiedsCategory){
		$sql = 'INSERT INTO classifieds_category (category_name, description, created_date, updated_date) VALUES ("'.$classifiedsCategory->category_name.'", "'.$classifiedsCategory->description.'", "'.$classifiedsCategory->created_date.'", "'.$classifiedsCategory->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$classifiedsCategory->category_id = $id;
		return $id;
	}
	
	public function insertSubcategory($classifiedsCategory){
		$sql = 'INSERT INTO classifieds_category (category_name, description, parent_id, created_date, updated_date) VALUES ("'.$classifiedsCategory->category_name.'", "'.$classifiedsCategory->description.'", "'.$classifiedsCategory->parent_id.'", "'.$classifiedsCategory->created_date.'", "'.$classifiedsCategory->updated_date.'")';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$classifiedsCategory->category_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param ClassifiedsCategoryMySql classifiedsCategory
 	 */
	public function update($classifiedsCategory){
		$sql = 'UPDATE classifieds_category SET category_name = ?, description = ?, status = ?, updated_date = ? WHERE category_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($classifiedsCategory->category_name);
		$sqlQuery->set($classifiedsCategory->description);
		$sqlQuery->setNumber($classifiedsCategory->status);
		$sqlQuery->set($classifiedsCategory->updated_date);
		$sqlQuery->setNumber($classifiedsCategory->category_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	public function updateSubcategory($classifiedsCategory){
		$sql = 'UPDATE classifieds_category SET category_name = ?, description = ?, status = ?, updated_date = ? WHERE category_id = ? and parent_id > 0';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($classifiedsCategory->category_name);
		$sqlQuery->set($classifiedsCategory->parent_id);
		$sqlQuery->set($classifiedsCategory->description);
		$sqlQuery->setNumber($classifiedsCategory->status);
		$sqlQuery->set($classifiedsCategory->updated_date);
		$sqlQuery->setNumber($classifiedsCategory->category_id);
		//print_r($sqlQuery);exit;
		return $this->executeUpdate($sqlQuery);
	}

	
	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM classifieds_category';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByCategoryName($value){
		$sql = 'SELECT * FROM classifieds_category WHERE category_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByParentId($value){
		$sql = 'SELECT * FROM classifieds_category WHERE parent_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescription($value){
		$sql = 'SELECT * FROM classifieds_category WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM classifieds_category WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM classifieds_category WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM classifieds_category WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByCategoryName($value){
		$sql = 'DELETE FROM classifieds_category WHERE category_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByParentId($value){
		$sql = 'DELETE FROM classifieds_category WHERE parent_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescription($value){
		$sql = 'DELETE FROM classifieds_category WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM classifieds_category WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM classifieds_category WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM classifieds_category WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return ClassifiedsCategoryMySql 
	 */
	protected function readRow($row){
		$classifiedsCategory = new ClassifiedsCategory();
		
		$classifiedsCategory->category_id = $row['category_id'];
		$classifiedsCategory->category_name = $row['category_name'];
		$classifiedsCategory->parent_id = $row['parent_id'];
		$classifiedsCategory->description = $row['description'];
		$classifiedsCategory->status = $row['status'];
		$classifiedsCategory->created_date = $row['created_date'];
		$classifiedsCategory->updated_date = $row['updated_date'];

		return $classifiedsCategory;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ClassifiedsCategoryMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>